package com.settlement.system.parse.formula;

import com.alibaba.excel.util.DateUtils;
import com.settlement.system.parse.lang.OrderDate;

import java.util.Date;

/**
 * 日期类型的计算
 *
 * @author Ximi
 * @since 2020/10/12
 */
public class DateCalculator extends AbstractOperatorCalculator<OrderDate> {

    public DateCalculator(PlaceHoldParse placeHoldParse) {
        super(placeHoldParse);
    }

    @Override
    public OrderDate addition(OrderDate a, OrderDate b) {
        return a.addition(b);
    }

    @Override
    public OrderDate substract(OrderDate a, OrderDate b) {
        return a.substract(b);
    }

    @Override
    public OrderDate multiply(OrderDate a, OrderDate b) {
        throw new UnSupportOperationalException("no support multiply operational");
    }

    @Override
    public OrderDate devide(OrderDate a, OrderDate b) {
        throw new UnSupportOperationalException("no support divide operational");
    }

    @Override
    public OrderDate conver(String arg) {
        final String y = "y";
        final String m = "m";
        final String d = "d";

        if (arg.contains(y)) {
            String year = arg.substring(0, arg.indexOf(y));
            return new OrderDate(Integer.valueOf(year), 0, 0);
        }

        if (arg.contains(m)) {
            String month = arg.substring(0, arg.indexOf(m));
            return new OrderDate(0, Integer.valueOf(month), 0);
        }

        if (arg.contains(d)) {
            String day = arg.substring(0, arg.indexOf(d));
            return new OrderDate(0, 0, Integer.valueOf(day));
        }

        try {
            Date date = DateUtils.parseDate(arg, "yyyyMMdd");
            return new OrderDate(date);
        } catch (Exception ex) {

        }
        return null;
    }

}
